import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core';
import { I18nService } from '../../core/i18n/i18n.service';

@Component({
    selector: 'app-wallet-table',
    templateUrl: './table-basic.html',
    styleUrls: ['./table-basic.scss']
})
export class TableComponent implements OnInit {
    private _dataFilter:any;

    @Input()
    data: Array<any>;
    @Input()
    isSel: boolean = true;

    @Input()
    page: any;

    @Input()
    titleData: Array<cellConfig>;

    @Output()
    tabelEvent: EventEmitter<any> = new EventEmitter<any>();

    @Input()
    get dataFilter() {
        return this._dataFilter
    };
    set dataFilter(value:any) {
        console.log(value)
        this._dataFilter = value;
        this.filter(this.data,value.name,'ks');
    };


    searchAddress: string;
    sortName: string | null = null;
    sortValue: string | null = null;
    listOfSearchName: string[] = [];
    listOfDisplayData: Array<any> = [
        this.data
      ];

    constructor(private i18nService: I18nService) {}

    changeLang(lang: string) {
        this.i18nService.changeLang(lang);
    }
    private _fristData:Array<any>;
    ngOnInit() {
        this._fristData = this.data;
    }

    onClick(name, data) {
        const obj: any = { data: data };
        Object.assign(obj, name);
        this.tabelEvent.emit(obj);
    }

    changePage(pageNo, pageData): void {
        pageData.pageNo = pageNo;
        this.tabelEvent.emit(pageData);
    }
    filter(listOfSearchName: string[], searchAddress: string,key:string): void {
        // this.listOfSearchName = listOfSearchName;
        this.searchAddress = searchAddress;
        this.search(key);
    }
    search(key:string): void {
        this.data = this._fristData;
        /** filter data **/
        const filterFunc = (item ) =>
          (this.searchAddress ? item[key].indexOf(this.searchAddress) !== -1 : true) ;
          
        const data = this.data.filter(item => filterFunc(item));
        this.data = data;
      }
}
/**
 * @name String 按钮描述名称
 * @type String 按钮功能类型（click，gotoPage）
 * @show Function 按钮是否显示，传入当前行数据对象，返回一个布尔值
 */
export interface labelConfig {
    name: string;
    type: string;
    show?: any;
}
/**
 * @name String 列的描述名称
 * @class String 列的class样式名 
 * @key String 列的key
 * @labels labelConfig 按钮定义
 */
export interface cellConfig {
    name: string;
    class?: string;
    key: string;
    labels?: Array<labelConfig>;
}